#ifndef BINARYSTREAMREADER_H
#define BINARYSTREAMREADER_H
#include <string>

#ifndef min
#define min(a,b)            (((a) < (b)) ? (a) : (b))
#endif

enum
{
    TEXT_PACKLEN_LEN = 4,
    TEXT_PACKAGE_MAXLEN = 0xffff,
    BINARY_PACKLEN_LEN = 2,
    BINARY_PACKAGE_MAXLEN = 0xffff,

    TEXT_PACKLEN_LEN_2 = 6,
    TEXT_PACKAGE_MAXLEN_2 = 0xffffff,

    BINARY_PACKLEN_LEN_2 = 4,               //4字节头长度
    BINARY_PACKAGE_MAXLEN_2 = 0x10000000,   //包最大长度是256M,足够了

    CHECKSUM_LEN = 2,
};

namespace  net{

class BinaryStreamReader final
{
public:
    BinaryStreamReader(const char* ptr, size_t len);
    ~BinaryStreamReader() = default;

    virtual const char* GetData() const;
    virtual size_t GetSize() const;
    bool IsEmpty() const;
    bool ReadString(std::string* str, size_t maxlen, size_t& outlen);
    bool ReadCString(char* str, size_t strlen, size_t& len);
    bool ReadCCString(const char** str, size_t maxlen, size_t& outlen);
    bool ReadInt32(int32_t& i);
    bool ReadInt64(int64_t& i);
    bool ReadShort(short& i);
    bool ReadChar(char& c);
    size_t ReadAll(char* szBuffer, size_t iLen) const;
    bool IsEnd() const;
    const char* GetCurrent() const { return cur; }

public:
    bool ReadLength(size_t& len);
    bool ReadLengthWithoutOffset(size_t& headlen, size_t& outlen);

private:
    BinaryStreamReader(const BinaryStreamReader&) = delete;
    BinaryStreamReader& operator=(const BinaryStreamReader&) = delete;

private:
    const char* const ptr;
    const size_t      len;
    const char* cur;
};
}


#endif // BINARYSTREAMREADER_H
